//
//  ReportView.swift
//  skincare
//
//  Created by mac on 2023/5/18.
//

import SwiftUI
import SwiftUICharts

struct SelectView: View {
    
    let Num_Report:Int = 12
    let Days_Report:Int = 112
    
    var body: some View {
        VStack{
            ZStack{
                VStack(alignment:.leading, spacing: 10){
                    HStack{
                        Text("欢迎来到")
                            .font(.system(size: 30))
                        Spacer()
                    }

                    HStack{
                        Text("趣痘精灵用户的第")
                            .font(.system(size: 20))
                        Text("\(Num_Report)")
                            .font(.system(size: 25))
                            .fontWeight(.semibold)
                        Text("份报告")
                            .font(.system(size: 20))
                    }
                    
                    Text("\(Date(), style: .date)")
                        .font(.system(size: 18))
                        .padding(.bottom)
                    
                    HStack{
                            Rectangle()
                                .fill(Color.white)
                                .overlay(
                                    ZStack{
                                        Circle()
                                            .trim(from: 0.2, to: 1.0)
                                            .stroke(Color.green.opacity(0.5), lineWidth: 20)
                                            .frame(width: 120, height: 120)
                                            .rotationEffect(Angle(degrees: 54))
                                        VStack{
                                            HStack(spacing:3){
                                                Text("\(Days_Report)")
                                                    .font(.system(size: 40))
                                                    .fontWeight(.semibold)
                                                Text("Days")
                                                    .font(.system(size: 20))
                                            }
                                            Text("您已坚持打卡")
                                                .font(.system(size: 20))
                                        }
                                    }
                                )
                                .overlay(
                                    Rectangle()
                                        .stroke(Color.green, lineWidth: 1.5)
                                )

                        VStack{
                            //跳转页面
                            NavigationLink(destination: EmptyView()){
                                Rectangle()
                                    .fill(Color.white)
                                    .overlay(
                                        HStack{
                                            VStack(alignment: .leading, spacing:5){
                                                Text("您的计划")
                                                    .fontWeight(.semibold)
                                                    .foregroundColor(Color("deepGreen"))
                                                    .font(.system(size: 18))
                                                Text("3天焕肤计划")
                                                    .foregroundColor(Color.black)
                                                    .font(.system(size: 18))
                                            }
                                            .padding(.leading, 10)
                                            Spacer()
                                        }
                                    )
                                    .overlay(
                                        Rectangle()
                                            .stroke(Color.green, lineWidth: 1.5)
                                    )
                                }
                            
                            //跳转页面
                            NavigationLink(destination: EmptyView()){
                                Rectangle()
                                    .fill(Color.white)
                                    .overlay(
                                        HStack{
                                            VStack(alignment: .leading, spacing:5){
                                                Text("您的产品")
                                                    .fontWeight(.semibold)
                                                    .foregroundColor(Color("deepGreen"))
                                                    .font(.system(size: 18))
                                                Text("点击选取分享")
                                                    .foregroundColor(Color.black)
                                                    .font(.system(size: 18))
                                            }
                                            .padding(.leading, 10)
                                            Spacer()
                                        }
                                    )
                                    .overlay(
                                        Rectangle()
                                            .stroke(Color.green, lineWidth: 1.5)
                                    )
                                }
                            }
                    }.padding(-10)
                }
                .padding(30)
                .frame(height: 335)
            }
            .padding(.top, 50)
            .background(
                RoundedRectangle(cornerRadius: 50)
                    .fill(Color("systemColor"))
                    .frame(height: 500)
            )
            .ignoresSafeArea(.all)
            Spacer()
        }
        
    }
}


struct ScoreView: View {
    // 数据部分
    let Score_All:[Double] = [86]
    let Score_Acne:[Double] = [100]
    let Score_Comedo:[Double] = [98]
    let Score_Oil:[Double] = [60]
    let Num_Acne:Int = 0
    let Num_Comedo:Int = 7
    
    let TextName: [String] = ["总结", "痘痘", "黑头", "肤色", "护肤方案"]
    var Scores: [[Double]] {[[Double(mySkinData.Score_All)],
                             [Double(mySkinData.Score_Acne)],
                             [Double(mySkinData.Score_Comedo)],
                             [Double(mySkinData.Score_Oil)],
                             [Double(mySkinData.Score_All)]]}
    var Texts: [String] {["您的肤质较好，请继续保持！", "痘痘数量：", "黑头数量：", "您的肤质较好，请继续保持！",
                          "1.温和清洁：选择温和的洁面产品，避免使用含有刺激性成分的产品，并且不要过度清洁。每天早晚各清洁一次就足够了。2.保湿：使用保湿产品来补充皮肤所需的水分。可选择含有透明质酸、甘油或天然优质的产品。3.防晒：在外面活动时，应该使用防晒霜来保护皮肤免受紫外线伤害。"]}
    
    @Binding var selection: Int
    let Data: [Double] = [86]
    
    var body: some View {
        
        Rectangle()
            .fill(.white)
            //.frame(height:400)
            .overlay(
                VStack(spacing:0){
                    HStack{
                        Capsule()
                            .foregroundColor(Color("systemColor"))
                            .frame(width:110, height:30)
                            .overlay(
                                Text(TextName[selection])
                                    .font(.system(size: 18))
                            )
                            .padding(.leading, 5)
                            .padding(5)
                        Spacer()
                    }
                
                    HStack{
                        RingsChart()
                            .data(Scores[selection])
                            .chartStyle(ChartStyle(backgroundColor: .white,
                                                   foregroundColor: ColorGradient(.green, .green)))
                            .frame(width: 130)
                            .overlay(
                                VStack{
                                    Text("\(Int(Scores[selection][0]))")
                                        .font(.system(size: 30))
                                        .foregroundColor(Color.green)
                                    Text("分数")
                                        .font(.system(size: 20))
                                }
                            )
                        ScrollView{
                            VStack(spacing:0){
                                if selection==1 || selection==2 {
                                    Text(Texts[selection])
                                        .font(.system(size: 20))
                                    selection==1 ? Text("\(Num_Acne) ").font(.system(size: 30))
                                                 : Text("\(Num_Comedo) ").font(.system(size: 30))
                                } else {
                                    Text(Texts[selection])
                                }
                                
                                if selection == 3{
                                    Image(systemName: "skinColor")
                                        .frame(width:50, height:50)
                                }
                            }
                            .frame(width:200)
                        }
                        .frame(height:130)
                    }
                    .frame(height:200)
                    
                    // 跳转界面
                    NavigationLink(destination: EmptyView(), label:{
                        Capsule()
                            .foregroundColor(Color.green)
                            .frame(width:170, height:50)
                            .overlay(
                                Text("生成宠物形象")
                                    .font(.system(size: 18))
                                    .foregroundColor(Color.white)
                            )
                    })
                    Spacer()
                }
            )
    }
}


struct ReportView: View {
    
    @State var selection: Int = 0
    
    init() {
        UITabBar.appearance().backgroundColor = UIColor(named: "systemColor") //设置背景色，否则背景色为透明颜色
        UITabBar.appearance().unselectedItemTintColor = UIColor(Color.black.opacity(0.7))
    }
    
    var body: some View {
        VStack{
            //假tabview，实际通过selection来改变视图
            TabView(selection: $selection){
                SelectView()
                    .tabItem {
                        Image(systemName: "person.fill.viewfinder")
                        Text("总结")
                    }
                    .tag(0)
                SelectView()
                    .tabItem {
                        Image(systemName: "light.beacon.min")
                        Text("痘痘")
                    }
                    .tag(1)
                SelectView()
                    .tabItem {
                        Image(systemName: "nose")
                        Text("黑头")
                    }
                    .tag(2)
                SelectView()
                    .tabItem {
                        Image(systemName: "circle.righthalf.filled")
                        Text("肤色")
                    }
                    .tag(3)
                SelectView()
                    .tabItem {
                        Image(systemName: "list.clipboard")
                        Text("护肤方案")
                    }
                    .tag(4)
            }
            .accentColor(.green)
            
            ScoreView(selection: $selection)
        }
    }
}

// 声明全局变量
var mySkinData = SkinData(if_face: 1, if_nose: 1, Color: 1, Acne: 0, Comedo: 0, Oil: 0, Score_Acne: 100, Score_Comedo: 98, Score_Oil: 60, Num_Acne: 0, Num_Comedo: 7, Index_Oil: 0, Score_All: 86)

func updateValue(_ newValue: SkinData) { // setter 函数
    mySkinData = newValue
}

struct ReportView_Previews: PreviewProvider {
    
    //等待传入的数据
    
    static var previews: some View {
        ReportView()
    }

}
